package com.jianyun.jyzs.http;


import android.util.Log;

import com.jianyun.jyzs.utils.LoginCache;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by liml on 17/2/28.
 */

public class RetrofitHelper {

    private static Retrofit retrofitroot;//请求根
    private static Retrofit retrofitroot2;//请求根bcHttpUrl
    private static Retrofit retrofitroot3;//请求根erpRootUrl
    private static Retrofit retrofitroot4;//请求根erphttpUrl
    private static Retrofit retrofitroot5;//阿里发票查询请求根
    private static Retrofit retrofitroot6;//新版签到请求
    private static Retrofit retrofitroot7;//新版签到请求
    private static Retrofit retrofitroot8;//新版签到请求
    private static Retrofit retrofitroot9;//上传头像文件接口
    private static Retrofit retrofitroot10;//上传头像文件接口
    //调试新版签到接口 URL
    private static String url = "http://demo.pmcloud.cn/pmone_debug/DataService/";
    private static String url2 = "http://192.168.0.96:803/";

    public synchronized static Retrofit builder() {
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        // if (retrofitroot != null) return retrofitroot;
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        LInterceptor lInterceptor = new LInterceptor();
        builder.addInterceptor(loggingInterceptor);
        builder.readTimeout(20000, TimeUnit.MILLISECONDS);
        retrofitroot = new Retrofit.Builder().baseUrl(Api.centerUrl)
                .client(builder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        return retrofitroot;
    }

    public static void clear() {
        retrofitroot = null;
        retrofitroot2 = null;
        retrofitroot3 = null;
        retrofitroot4 = null;

    }


    /**
     * bchttpurl
     */
    /*public synchronized static Retrofit builderbch() {
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        Log.i("test",LoginCache.getInstance().getLoginCache().toString());
        //if (retrofitroot2 != null) return retrofitroot2;
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        LInterceptor lInterceptor = new LInterceptor();
        builder.addInterceptor(loggingInterceptor);
        builder.readTimeout(20000, TimeUnit.MILLISECONDS);
        retrofitroot2 = new Retrofit.Builder().baseUrl(LoginCache.getInstance().getLoginCache().getBcHttpUrl() + "/")
                .client(builder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        return retrofitroot2;
    }*/

    /**
     * @return erprootUrl
     */
    public synchronized static Retrofit builderErpRoot() {
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        // if (retrofitroot3 != null) return retrofitroot3;
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        LInterceptor lInterceptor = new LInterceptor();
        builder.addInterceptor(loggingInterceptor);
        builder.readTimeout(20000, TimeUnit.MILLISECONDS);
        retrofitroot3 = new Retrofit.Builder().baseUrl(LoginCache.getInstance().getLoginCache().getErpRootUrl())
                .client(builder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        Log.i("test", "erpRoot:" + LoginCache.getInstance().getLoginCache().getErpRootUrl());
        return retrofitroot3;
    }

    /**
     * @return erprootUrl
     */
    public synchronized static Retrofit builderErpRoot2() {
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        // if (retrofitroot9 != null) return retrofitroot9;
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.readTimeout(20000, TimeUnit.MILLISECONDS);
        retrofitroot9 = new Retrofit.Builder().baseUrl(LoginCache.getInstance().getLoginCache().getErpRootUrl())
                .client(builder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        //Log.i("test", "erpRoot:" + LoginCache.getInstance().getLoginCache().getErpRootUrl());
        return retrofitroot9;
    }

    /**
     * @return erprootUrl
     */
    public synchronized static Retrofit builderCeshiHttp() {
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        // if (retrofitroot7 != null) return retrofitroot7;
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        LInterceptor lInterceptor = new LInterceptor();
        builder.addInterceptor(loggingInterceptor);
        builder.readTimeout(20000, TimeUnit.MILLISECONDS);
        retrofitroot7 = new Retrofit.Builder().baseUrl(LoginCache.getInstance().getLoginCache().getErpRootUrl())
                .client(builder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        Log.i("test", "url2:" + LoginCache.getInstance().getLoginCache().getErpRootUrl());
        return retrofitroot7;
    }

    /**
     * erphttpurl
     */
    public synchronized static Retrofit builderErpHttp() {
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        //if (retrofitroot4 != null) return retrofitroot4;
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        LInterceptor lInterceptor = new LInterceptor();
        builder.addInterceptor(loggingInterceptor);
        builder.readTimeout(20000, TimeUnit.MILLISECONDS);
        retrofitroot4 = new Retrofit.Builder().baseUrl(LoginCache.getInstance().getLoginCache().getErpHttpUrl() + "/")
                .client(builder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        Log.i("test", "erpHttp:" + LoginCache.getInstance().getLoginCache().getErpHttpUrl());
        return retrofitroot4;
    }

    /**
     * 新版签到接口
     */
    public synchronized static Retrofit builderNewSignHttp() {
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        //if (retrofitroot6 != null) return retrofitroot6;
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        LInterceptor lInterceptor = new LInterceptor();
        builder.addInterceptor(loggingInterceptor);
        builder.readTimeout(20000, TimeUnit.MILLISECONDS);
        retrofitroot6 = new Retrofit.Builder().baseUrl(LoginCache.getInstance().getLoginCache().getErpHttpUrl())
                .client(builder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        return retrofitroot6;
    }

    /**
     * 新版签到接口
     */
    public synchronized static Retrofit builderNewSign2Http() {
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        //if (retrofitroot8 != null) return retrofitroot8;
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        LInterceptor lInterceptor = new LInterceptor();
        builder.addInterceptor(loggingInterceptor);
        builder.readTimeout(20000, TimeUnit.MILLISECONDS);
        retrofitroot8 = new Retrofit.Builder().baseUrl(url)
                .client(builder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        //Log.i("test", "新版签到:" + url);
        return retrofitroot8;
    }

    /**
     * 阿里发票查询
     */
    public synchronized static Retrofit builderFpQuesthttp() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        LInterceptor lInterceptor = new LInterceptor();
        builder.addInterceptor(lInterceptor);
        builder.readTimeout(20000, TimeUnit.MILLISECONDS);
        retrofitroot5 = new Retrofit.Builder().baseUrl("https://fapiao.market.alicloudapi.com/")
                .client(builder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        return retrofitroot5;
    }

    /**
     * 顶部轮播图
     */
    public synchronized static Retrofit builderTopImghttp() {
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
       // LInterceptor lInterceptor = new LInterceptor();
        builder.addInterceptor(loggingInterceptor);
        builder.readTimeout(20000, TimeUnit.MILLISECONDS);
        retrofitroot10 = new Retrofit.Builder().baseUrl("https://fapiao.market.alicloudapi.com/")
                .client(builder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        return retrofitroot10;
    }

    static HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
        @Override
        public void log(String message) {
            //loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            //打印retrofit日志
            Log.d("RetrofitLog", "retrofitBack ==>" + message);
        }
    });

}
